using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BusinessLayer;
using EntityLayer;
using WebControls;

public partial class Admin_Entries : SFPage
{
    busEntries insBusEntries = new busEntries();
    entEntries insEntEntries = new entEntries();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgEntries_NeedsDataSource()
    {
        DataTable insDataTableEntries = insBusEntries.SelectEntries();
        if (insBusEntries.HasErrors)
        {
            MessageBoxShow(insBusEntries);
        }
        sfgEntries.SetData(insDataTableEntries);
        return insBusEntries.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgEntries_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntEntries.EntryId = Convert.ToInt32(e.CommandArgument);
            insBusEntries.DeleteEntriesByEntryId(insEntEntries);
            if (insBusEntries.HasErrors)
            {
                MessageBoxShow(insBusEntries);
                return;
            }
            sfgEntries.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstEntries.LoadDataListData();

    }
    protected bool lstEntries_NeedsDataSource()
    {
        busCategories insBusCategories = new busCategories();
        DataTable insDataTableCategories = insBusCategories.SelectCategoriesByLanguageId();
        if (insBusCategories.HasErrors)
        {
            MessageBoxShow(insBusCategories);
            return true;
        }
        sfcEntryCategories.SetData(insDataTableCategories, "CategoryName", "CategoryId");

        entEntryCategories insEntEntryCategories = new entEntryCategories();
        insEntEntryCategories.EntryId = this.Id;
        busEntryCategories insBusEntryCategories = new busEntryCategories();
        DataTable insDataTableEntryCategories = insBusEntryCategories.SelectEntryCategoriesByEntryId(insEntEntryCategories);
        if (insBusEntryCategories.HasErrors)
        {
            MessageBoxShow(insBusEntryCategories);
            return true;
        }
        sfcEntryCategories.SetItemsChecked(insDataTableEntryCategories);

        insEntEntries.EntryId = this.Id;
        insBusEntries.SelectEntriesByEntryId(insEntEntries);
        if (insBusEntries.HasErrors)
        {
            MessageBoxShow(insBusEntries);
            return true;
        }
        txtEntryDescription.Text = insEntEntries.EntryDescription;
        calPublishDate.Value = insEntEntries.PublishDate;
        chkAllowComments.Checked = insEntEntries.AllowComments;

        entEntryContents insEntEntryContents = new entEntryContents();
        insEntEntryContents.EntryId = insEntEntries.EntryId;
        busEntryContents insBusEntryContents = new busEntryContents();
        DataTable insDataTableEntryContents = insBusEntryContents.SelectEntryContentsByEntryId(insEntEntryContents);
        if (insBusEntryContents.HasErrors)
        {
            MessageBoxShow(insBusEntryContents);
            return true;
        }
        lstEntries.SetData(insDataTableEntryContents);
        return false;
    }

    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

        insEntEntries.EntryDescription = txtEntryDescription.Text;
        insEntEntries.PublishDate = calPublishDate.Value.Value;
        insEntEntries.InsertDate = calPublishDate.Value.Value;
        insEntEntries.AllowComments = chkAllowComments.Checked;

        foreach (ListItem li in sfcEntryCategories.Items)
        {
            if (li.Selected)
            {
                entEntryCategories insEntEntryCategories = new entEntryCategories();
                insEntEntryCategories.CategoryId = Convert.ToInt32(li.Value);
                insEntEntries.EntryCategories.Add(insEntEntryCategories);
            }
        }

        foreach (DataListItem dli in lstEntries.Items)
        {
            entEntryContents insEntEntryContents = new entEntryContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntEntryContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntEntryContents.Body = ((SFEditor)dli.FindControl("ediBody")).HTML;
                insEntEntryContents.BodyText = ((SFEditor)dli.FindControl("ediBody")).Text;
                insEntEntryContents.Title = ((SFTextBox)dli.FindControl("txtTitle")).Text;
                if (lstEntries.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntEntryContents.EntryContentId = Convert.ToInt32(lstEntries.DataKeys[dli.ItemIndex]);
                    insEntEntries.EntryContentsUpdate.Add(insEntEntryContents);
                }
                else
                {
                    insEntEntries.EntryContentsInsert.Add(insEntEntryContents);
                }
            }
            else
            {
                if (lstEntries.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntEntryContents.EntryContentId = Convert.ToInt32(lstEntries.DataKeys[dli.ItemIndex]);
                    insEntEntries.EntryContentsDelete.Add(insEntEntryContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusEntries.InsertEntries(insEntEntries);
        if (insBusEntries.HasErrors)
        {
            MessageBoxShow(insBusEntries);
            return;
        }
        base.SaveNew();
        sfgEntries.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntEntries.EntryId = this.Id;
        insBusEntries.UpdateEntriesByEntryId(insEntEntries);
        if (insBusEntries.HasErrors)
        {
            MessageBoxShow(insBusEntries);
            return;
        }
        base.SaveExisting();
        sfgEntries.LoadGridData();
    }

    protected void lstEntries_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["EntryContentId"] != DBNull.Value);
                e.Item.FindControl("pnlEntriesContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }
    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlEntriesContents").Visible = ((SFCheckBox)sender).Checked;
    }
}

